classdef dotsPlayableFile < dotsPlayable
    % @class dotsPlayableFile
    % Play a sound from a .wav or .mp3 file
    
    properties (SetObservable = true)
        % name of file you want to play
        fileName
        % path to sound file
        filePath   
        % so far we only support .wav or .mp3...
        fileType
    
    
    end
    
    properties (SetObservable = false)
        % Matlab audioplayer object
        player;
        
 
    end
    
    methods
        function self = dotsPlayableFile
            self = self@dotsPlayable;
        end
 
        function prepareToPlay(self)            
            fileType=self.fileName(end-3:end);
            % get waveform and properties of .wav or .mp3 files
            if strcmp(fileType, '.wav')
                % MATLAB's builtin .wav reader
                [self.waveform, self.sampleFrequency, self.bitsPerSample] = ...
                    wavread(self.fileName);
            elseif strcmp(sfileType, '.mp3')
                % mp3read function in DotsX/mex
                % BSH downloaded 2 May 2008 from MATLAB Central File Exchange:
                % http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=13852&objectType=file
                [self.waveform, self.sampleFrequency, self.bitsPerSample] = ...
                    mp3read(self.fileName);
            else
                disp(sprintf('is %s a .wav or .mp3 file?', ...
                    self.fileName));
            end
            self.player = audioplayer(self.waveform, ...
                self.sampleFrequency, self.bitsPerSample);
        end
        
        
        function play(self)
            if isobject(self.player)
                % play is async, playblocking would be sync
                play(self.player);
            end
        end
    end
end